import store from './store'
let isAndroidBack=false;
function matchingRoute(routes, name) {
  return routes.filter(route => {
    if (route.name == name) {
      return true;
    }
    if (route.children) {
      matchingRoute(route.children, name)
    }
  })
}

function matchingCache(name) {
  return store.getters.cache.some(role => role === name)
}

function delCache(name) {
  store.dispatch("DEL_CACHR_NAME", name)
}

function add_nav(name) {
  store.dispatch("ADD_NAV", name)
}

function del_nav_index() {
  store.dispatch("DEL_NAV", "")
  if (store.getters.nav.length > 0) {
    return store.getters.nav[store.getters.nav.length - 1]
  } else {
    return ""
  }
}

function add_cache(name) {

  store.dispatch("ADD_CACHE_NAME", name)
}

function matchingNav() {
  return store.getters.nav.some(role => role === name)
}
export default {
  install(Vue) {
    var s = this;
    Vue.prototype.assign = function (target, source) {
        for (var obj in source) {
          if (typeof source[obj] == "object" && typeof target[obj] == "object" && source[obj]) {
            this.assign(target[obj], source[obj]);
          } else {
            if (source[obj] || source[obj] == 0) {
              target[obj] = source[obj];
            }
          }
        }
        return target;
      },
      Vue.prototype.navigation_push = function (name, params, components) {
        store.commit('updateDirection', {
            direction: 'forward'
          })
        let route = matchingRoute(this.$router.options.routes, name)
        if (matchingCache(name)) {
          delCache(name)
        }
        add_nav(name)
        this.$router.push({
          path: route[0].path,
          query: params
        })
      },
      Vue.prototype.navigation_pop = function () {
        
          store.commit('updateDirection', {
            direction: 'back'
          })
        let nav = JSON.parse(JSON.stringify(store.getters.nav));
        if(nav.length>0){
         nav=nav[nav.length-1]
        }
        let lastNav = del_nav_index();    
        if (lastNav) {
          let route = matchingRoute(this.$router.options.routes, nav)
          if (!matchingCache(route[0].name)) {
            add_cache(route[0].name)
          }
        } else {
            add_cache(nav)
        }
        isAndroidBack=true;
        this.$router.go(-1);
        
      },
      Vue.prototype.navigation_android_back = function () {
         if(isAndroidBack){
            isAndroidBack=false
            return;
         }
         store.commit('updateDirection', {
          direction: 'back'
        })
       
      let nav = JSON.parse(JSON.stringify(store.getters.nav));
      if(nav.length>0){
       nav=nav[nav.length-1]
      }
      let lastNav = del_nav_index();    
      if (lastNav) {
        let route = matchingRoute(this.$router.options.routes, nav)
        if (!matchingCache(route[0].name)) {
          add_cache(route[0].name)
        }
      } else {
          add_cache(nav)
      }
    }
  }
}
